/*
*	Copyright (c) 2009 Brent Mombourquette
*
*	Permission is hereby granted, free of charge, to any person
*	obtaining a copy of this software and associated documentation
*	files (the "Software"), to deal in the Software without
*	restriction, including without limitation the rights to use,
*	copy, modify, merge, publish, distribute, sublicense, and/or sell
*	copies of the Software, and to permit persons to whom the
*	Software is furnished to do so, subject to the following
*	conditions:
*
*	The above copyright notice and this permission notice shall be
*	included in all copies or substantial portions of the Software.
*
*	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
*	EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
*	OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
*	NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
*	HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
*	WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
*	FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
*	OTHER DEALINGS IN THE SOFTWARE.
*
*	This file contains the functions for the Graph tab of graph_page.html and their helper methods.
*/

const SHOW_TITLE = 0;			// The three options for what the node tooltip will display
const SHOW_THUMBNAIL = 1;
const SHOW_BOTH = 2;
var nodeDisplay = SHOW_TITLE;	// What will be displayed in the node tooltip
								// By default the title is shown
var graphTab = {
	/*
	 * Reload the graph.
	 * Globals: layout [from graph_page.html], init()[from graph_page.html], graphParticles [from manipulate_graph.js]
	 */
	reloadGraph: function() {
		// Determine if the current graph is a filtered graph
		var filterText = document.getElementById("filteringText").innerHTML;
		var filterSet = filterTab.isFiltered(filterText);
		// If a filter is applied then some of the nodes wont be there, and if the user moved only half of the nodes
		// around then if it saves those halves new positions the resulting graph, with all the nodes, will be messy
		if (!filterSet)  {
			// Prior to reloading make sure all the cords get saved
			manipulateGraph.saveCords();
			// Reset coords
			graphParticles = new Array();
		}
		layout.view.clear(); // Clear nodes and forces
		document.body.removeChild(layout.view.svg); // Remove all SVG objects
		sessionTrail.removeAllEntries(); // Remove all entries from the Session Panel
		init(); // Rebuild the graph
	},
	
	/*
	 * Reset the filtering tag and reload the graph.
	 * Globals: layout [from graph_page.html], init()[from graph_page.html], graphParticles [from manipulate_graph.js]
	 */
	reloadGraphButton: function() {
		// Determine if the current graph is a filtered graph
		var filterText = document.getElementById("filteringText").innerHTML;
		var filterSet = filterTab.isFiltered(filterText);
		// If a filter is applied then some of the nodes wont be there, and if the user moved only half of the nodes
		// around then if it saves those halves new positions the resulting graph, with all the nodes, will be messy
		if (!filterSet)  {
			// Prior to reloading make sure all the cords get saved
			manipulateGraph.saveCords();
			// Reset coords
			graphParticles = new Array();
		}
		var filteringTextTag = document.getElementById("filteringText");
		filteringTextTag.innerHTML = "Filtering...Nothing";
		var filterTextEntry = document.getElementById("filterTextArea");
		filterTextEntry.value = "";
		layout.view.clear(); // Clear nodes and forces
		document.body.removeChild(layout.view.svg); // Remove all SVG objects
		sessionTrail.removeAllEntries(); // Remove all entries from the Session Panel
		init(); // Rebuild the graph
	},
	
	/*
	 * Stop the animation of the graph.
	 * Globals: layout, forcesStopped		[both from graph_page.html]
	 */
	stopAnimation: function() {
		graphHTMLControl.closeContexts();
		layout.model.realStop();
		layout.model.timer.stop();
		forcesStopped = true;
	},

	/*
	 * Open an options menu.
	 * Globals: nodeDisplay
	 */
	optionsMenu: function() {
		graphHTMLControl.closeContexts();
		window.openDialog("chrome://BreadCrumbs/content/options_menu.xul", "Title", "chrome,centerscreen", {currentDisplay: nodeDisplay} );
	},
	
	/*
	 * Set node mouseover to show thumbnails.
	 * Globals: nodeDisplay
	 */
	changeToThumbnail: function() {
		nodeDisplay = SHOW_THUMBNAIL;
	},
	
	/*
	 * Set node mouseover to show titles.
	 * Globals: nodeDisplay
	 */
	changeToTitle: function() {
		nodeDisplay = SHOW_TITLE;
	},
	
	/*
	 * Set node mouseover to show both titles and thumbnails.
	 * Globals: nodeDisplay
	 */
	changeToBoth: function() {
		nodeDisplay = SHOW_BOTH;
	}
}